//
//  SettingViewController.m
//  Consume100
//
//  Created by eliot on 16/3/23.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "SettingViewController.h"
#import "GetIdentifyingViewController.h"
#import "LoginAndRegistViewController.h"
@interface SettingViewController ()<UITableViewDataSource, UITableViewDelegate>


@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *exitloginBtn;

@end

@implementation SettingViewController

+(SettingViewController*)CreatSettingViewController{
    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    SettingViewController *setting=[story instantiateViewControllerWithIdentifier:@"SettingViewController"];
    return setting;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.exitloginBtn.backgroundColor = [UIColor colorWithRed:255/255.0 green:88/255.0 blue:88/255.0 alpha:1];
    self.exitloginBtn.layer.cornerRadius = 10;
    self.exitloginBtn.layer.masksToBounds = YES;
    self.tableView.backgroundColor = [UIColor colorWithRed:232/255.0 green:233/255.0 blue:232/255.0 alpha:1];
    self.view.backgroundColor = [UIColor colorWithRed:232/255.0 green:233/255.0 blue:232/255.0 alpha:1];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.title = @"设置";
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


//点击退出登录
- (IBAction)clickExitLogBtn:(id)sender {
    [MBProgressHUD showMessage:@"正在退出"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 5;
    NSString *baseUrl = [NSString stringWithFormat:@"%@loginout.jhtml", MyBaseURL];
    [manager POST:baseUrl parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            LoginAndRegistViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginAndRegistViewController"];
            window.rootViewController = vc;
            [window makeKeyAndVisible];
            [MBProgressHUD hideHUD];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"链接超时,请检查网络"];
        });
    }];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return 2;
    }else {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_arrows"]];
    iv.frame = CGRectMake(0, 0, 18, 15);
    iv.contentMode = UIViewContentModeScaleAspectFit;
    cell.accessoryView = iv;
    if (indexPath.section == 0) {
        cell.textLabel.text = @"修改登录密码";
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
             cell.textLabel.text = @"常见问题";
        }else {
            cell.textLabel.text = @"服务与反馈";
        }
    }
    if (indexPath.section == 2) {
        cell.textLabel.text = @"关于我们";
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {//点击修改密码
        GetIdentifyingViewController *vc = [GetIdentifyingViewController CreatGetIdentifyingViewController];
        vc.viewTitle = @"修改密码";
        [self creatNav:vc];
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {//点击常见问题
            
        }else {//点击服务与反馈
            
        }
    }
    if (indexPath.section == 2) {//点击关于我们
        
    }

}

//创建模态导航控制器
-(void)creatNav:(UIViewController *)view{
    
    UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:view];
    nav.navigationBar.barTintColor = [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
    view.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
